﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using YJTMedicalAideManage.Cores.ApiModel;
using YJTMedicalAideManage.Cores.Globals;
using YJTMedicalAideManage.Cores.PusApiModel;

namespace YJTMedicalAideManage.Cores.Servers
{
    public class ApiService
    {
        private readonly HttpClient _httpClient;
        public static string baseUrl = GlobalApp.Instance.BaseUrl;
        public ApiService()
        {
            _httpClient = new HttpClient();
        }
        /// <summary>
        /// 第一道登录接口
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<ApiLogin> LoginAsync(string username, string password)
        {
            var url = $"{baseUrl}api_back/api_login/login";
            var loginRequest = new
            {
                username,
                password,
                client = "danxi"
            };

            var jsonContent = JsonConvert.SerializeObject(loginRequest);
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ApiLogin>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }

        public async Task<ApiLoginSet> LoginSetAsync(string username, string password)
        {
            var url = $"{baseUrl}api_back/api_login/login_set_data";
            var loginRequest = new
            {
                username,
                password
            };

            var jsonContent = JsonConvert.SerializeObject(loginRequest);
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ApiLoginSet>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }

        public async Task<ApiAssInit> AssInitAsync(assinit assinit)
        {
            var url = $"{baseUrl}api_back/api_assistant/ass_init";


            var jsonContent = JsonConvert.SerializeObject(assinit);
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ApiAssInit>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }
        public async Task<ApiAssEditInit> AssEditInitAsync(string assinit)
        {
            var url = $"{baseUrl}api_back/api_assistant/ass_edit_init";


            var jsonContent = JsonConvert.SerializeObject(new { operator_id = assinit });
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ApiAssEditInit>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }

        public async Task<ApiAssAdd> AssAddAsync(assistantadd assinit)
        {
            var url = $"{baseUrl}api_back/api_assistant/ass_add";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(assinit);
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ApiAssAdd>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }
        public async Task<AssEdit> AssEditAsync(assistantadd assinit)
        {
            var url = $"{baseUrl}api_back/api_assistant/ass_edit";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(assinit);
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<AssEdit>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }

        public async Task<AssEdit> StateAsync(string operator_id)
        {
            var url = $"{baseUrl}api_back/api_assistant/ass_switch";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(new { operator_id = operator_id });
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<AssEdit>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }
        public async Task<AssEdit> TenantArrAsync(string ass_id, string[] tenant_arr = null)
        {
            var url = $"{baseUrl}api_back/api_assistant/ass_tenant_add";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(new
            {
                ass_id = ass_id,
                tenant_arr = new string[] { "738CE51D1E231C1A" }
        });
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<AssEdit>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }
        public async Task<ObjectAss> RelationDocAsync(string AssId, string[] DocArr = null)
        {
            var url = $"{baseUrl}api_back/api_assistant/RelationDoc";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(new
            {
                AssId = AssId,
                DocArr = new string[] { "594" }
            });
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ObjectAss>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }

        public async Task<ApiAddress> GetAllProvinceAsync()
        {
            var url = $"{baseUrl}api_back/api_system/get_all_province";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(new { });
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ApiAddress>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }
        public async Task<ApiAddress> GetAllCityAsync(string id)
        {
            var url = $"{baseUrl}api_back/api_system/get_all_city";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(new { provinceId = id });
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ApiAddress>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }
        public async Task<ApiAddress> GetAllAreaAsync(string id)
        {
            var url = $"{baseUrl}api_back/api_system/get_all_area";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(new { cityId = id });
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<ApiAddress>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }
        public async Task<AssEdit> logoutAsync(string id)
        {
            var url = $"{baseUrl}api_back/api_login/logout";

            // 序列化请求体
            var jsonContent = JsonConvert.SerializeObject(new {
                manager_id = id });
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 设置请求头
            _httpClient.DefaultRequestHeaders.Clear();
            _httpClient.DefaultRequestHeaders.Add("tokenmanagerid", GlobalApp.Instance.Manager_Id);
            _httpClient.DefaultRequestHeaders.Add("token", GlobalApp.Instance.Token);

            var response = await _httpClient.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var apiResponse = JsonConvert.DeserializeObject<AssEdit>(responseString);
                return apiResponse;
            }
            else
            {
                throw new Exception("API请求失败");
            }
        }
    }
}
